package com.saifan.wyy_ov.utils;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnKeyListener;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.saifan.wyy_ov.R;
import com.saifan.wyy_ov.ui.view.MaterialDialog;

/**
 * 浮动窗(对话框)显示工具类
 *
 * @author wangli
 */
public class DialogUtil {
    private static Dialog progressDialog;//进度框

    /**
     * @param con     上下文
     * @param content 显示的提示信息
     */
    @SuppressLint("NewApi")
    public static void showProgressDialog(final Context con, String content) {
        try {
            Activity act = (Activity) con;
            if (!act.isFinishing()) {
                progressDialog = new Dialog(con, R.style.dialog);
                progressDialog.setOnKeyListener(new OnKeyListener() {
                    @Override
                    public boolean onKey(DialogInterface dialog, int keyCode,
                                         KeyEvent event) {
                        switch (keyCode) {
                            case 4:
                                dialog.dismiss();
                                break;
                            default:
                                break;
                        }
                        return true;
                    }
                });
                progressDialog.setCanceledOnTouchOutside(false);
                progressDialog.show();
                Window win = progressDialog.getWindow();
                win.setContentView(R.layout.progress_bar);
                if (content != null && !content.isEmpty()) {
                    ((TextView) win.findViewById(R.id.progress_bar_dlg_content))
                            .setText(content);
                } else {
                    win.findViewById(R.id.progress_bar_dlg_content)
                            .setVisibility(View.GONE);
                }
            }
        } catch (IllegalArgumentException e) {
        } catch (Exception e) {
        }
    }

    /**
     * 关闭进度对话框
     */
    public static void closeProgressDialog() {
        try {
            if (progressDialog != null) {
                progressDialog.dismiss();
                progressDialog = null;
            }
        } catch (IllegalArgumentException e) {
        } catch (Exception e) {
        }
    }

    /**
     * @param context
     * @param view
     * @param title
     */
    public static MaterialDialog createDailog(Context context, View view, String title) {
        MaterialDialog dialog = new MaterialDialog(context);
        dialog.setCanceledOnTouchOutside(true);

        dialog.setTitle(title);
        dialog.setContentView(view);

        return dialog;
    }

    /**
     * @param context
     * @param title
     * @param msg
     */
    public static MaterialDialog createDailog(Context context, String title, String msg) {
        MaterialDialog dialog = new MaterialDialog(context);
        dialog.setCanceledOnTouchOutside(true);

        dialog.setTitle(title);
        dialog.setMessage(msg);

        return dialog;
    }

    /**
     * @param context
     * @param title
     * @param msg
     */
    public static MaterialDialog createDailog(Context context, int title, int msg) {
        MaterialDialog dialog = new MaterialDialog(context);
        dialog.setCanceledOnTouchOutside(true);

        dialog.setTitle(context.getString(title));
        dialog.setMessage(context.getString(msg));

        return dialog;
    }

    /**
     * inputMode=4 禁用输入法
     *
     * @param context
     */
    public static MaterialDialog createDailog(Context context, int inputMode) {
        MaterialDialog dialog = new MaterialDialog(context,inputMode);
        dialog.setCanceledOnTouchOutside(true);
        return dialog;
    }


}
